package com.bdth.bdthiss.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bdth.bdthiss.entity.*;
import com.bdth.bdthiss.service.ContractService;
import com.bdth.bdthiss.service.CustomerService;
import com.bdth.bdthiss.service.DepartService;
import com.bdth.bdthiss.service.PaymentService;
import com.bdth.bdthiss.util.DateUtil;
import com.bdth.bdthiss.vo.CommonList;
import com.bdth.bdthiss.vo.CommonResult;
import com.bdth.bdthiss.vo.EnhancedOption;
import com.bdth.bdthiss.vo.SysMoney;
import com.github.abel533.echarts.Option;
import com.github.abel533.echarts.Title;
import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.Line;
import com.github.abel533.echarts.series.MarkLine;
import com.github.abel533.echarts.series.MarkPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LGP
 * @since 2018-11-08
 */
@Controller
public class HomeController extends BaseController{

    @Autowired
    private DepartService departService;

    @Autowired
    private ContractService contractService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private PaymentService paymentService;

    @RequestMapping("/")
    public String index(HttpServletRequest request, HttpServletResponse response){
        User bean = (User) request.getSession().getAttribute("loginUser");
        if(bean==null){
            return "sys/login";
        }
        if(bean.getGroupid()==0)
            return "sys/admin";
        else
            return "sys/user";
    }

    @RequestMapping(value = "/portal/index")
    public String ind(){
        return "portal/index";
    }
    @RequestMapping(value = "/portal/home")
    public String home(){
        return "portal/home";
    }
    @RequestMapping(value = "/contract/years")
    public String years(){
        return "portal/years";
    }

    @RequestMapping(value = "/portal/map")
    public String map(){
        return "portal/map";
    }

    /**
     * 获取年内的合同签订数量
     * @return
     */
    @RequestMapping(value = "/contract/newCountractCount")
    @ResponseBody
    public CommonResult<String> newContract(){
        Integer year = LocalDate.now().getYear();
        String start = year + "-01-01";
        String end = year + "-12-31";
        EntityWrapper<Contract> entityWrapper = new EntityWrapper<>();
        entityWrapper.ge("crtime",start);
        entityWrapper.le("crtime", end);
        entityWrapper.eq("del",0);
        int newCountractValue = contractService.selectCount(entityWrapper);
        return CommonResult.success(String.valueOf(newCountractValue));
    }

    /**
     * 获取2019年内的合同签订数量
     * @return
     */
    @RequestMapping(value = "/contract/newCountractCount2019")
    @ResponseBody
    public CommonResult<String> newContract2019(){
        String start = 2019 + "-01-01";
        String end = 2019 + "-12-31";
        EntityWrapper<Contract> entityWrapper = new EntityWrapper<>();
        entityWrapper.ge("crtime",start);
        entityWrapper.le("crtime", end);
        entityWrapper.eq("del",0);
        int newCountractValue = contractService.selectCount(entityWrapper);
        return CommonResult.success(String.valueOf(newCountractValue));
    }

    /**
     * 获取合同签订总数量
     * @return
     */
    @RequestMapping(value = "/contract/allCountractCount")
    @ResponseBody
    public CommonResult<String> allContract(){
        EntityWrapper<Contract> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("del",0);
        int newCountractValue = contractService.selectCount(entityWrapper);
        return CommonResult.success(String.valueOf(newCountractValue));
    }

    /**
     * 获取客户总数量
     * @return
     */
    @RequestMapping(value = "/customer/allCustomerCount")
    @ResponseBody
    public CommonResult<String> allCustomers(){
        EntityWrapper<Customer> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("status",1);
        int allCustomerValue = customerService.selectCount(entityWrapper);
        return CommonResult.success(String.valueOf(allCustomerValue));
    }

    /**
     * 获取新增客户总数量
     * @return
     */
    @RequestMapping(value = "/customer/newCustomerCount")
    @ResponseBody
    public CommonResult<String> newCustomers(){
        Integer year = LocalDate.now().getYear();
        String start = year + "-01-01";
        String end = year + "-12-31";
        EntityWrapper<Customer> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("status",1);
        entityWrapper.ge("uptime",start);
        entityWrapper.le("uptime",end);
        int allCustomerValue = customerService.selectCount(entityWrapper);
        return CommonResult.success(String.valueOf(allCustomerValue));
    }

    /**
     * 获取2019年度新增客户总数量
     * @return
     */
    @RequestMapping(value = "/customer/newCustomerCount2019")
    @ResponseBody
    public CommonResult<String> newCustomers2019(){
        String start = 2019 + "-01-01";
        String end = 2019 + "-12-31";
        EntityWrapper<Customer> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("status",1);
        entityWrapper.ge("uptime",start);
        entityWrapper.le("uptime",end);
        int allCustomerValue = customerService.selectCount(entityWrapper);
        return CommonResult.success(String.valueOf(allCustomerValue));
    }

    /**
     * 获取已收和合同总金额
     * @return
     */
    @RequestMapping(value = "/contract/getMoney")
    @ResponseBody
    public CommonResult<SysMoney> getMoney(){
        EntityWrapper<Contract> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("del",0);
        List<Contract> list = contractService.selectList(entityWrapper);
        BigDecimal total = list.stream().map(Contract::getTotal).reduce(BigDecimal.valueOf(0), BigDecimal::add);
        BigDecimal receive = list.stream().map(Contract::getPayment).reduce(BigDecimal.valueOf(0), BigDecimal::add);
//        SysMoney bean = contractService.getMoney();
        SysMoney bean = new SysMoney(receive,total);
        return CommonResult.success(bean.transform());
    }

    /**
     * 获取2019年已收和合同总金额
     * @return
     */
    @RequestMapping(value = "/contract/getMoney2019")
    @ResponseBody
    public CommonResult<SysMoney> getMoney2019(){
        String start = 2019 + "-01-01";
        String end = 2019 + "-12-31";
        EntityWrapper<Contract> entityWrapper = new EntityWrapper<>();
        entityWrapper.ge("crtime",start);
        entityWrapper.le("crtime", end);
        entityWrapper.eq("del",0);
        List<Contract> list = contractService.selectList(entityWrapper);
        BigDecimal total = list.stream().map(Contract::getTotal).reduce(BigDecimal.valueOf(0), BigDecimal::add);
        BigDecimal receive = list.stream().map(Contract::getPayment).reduce(BigDecimal.valueOf(0), BigDecimal::add);
//        SysMoney bean = contractService.getMoney();
        SysMoney bean = new SysMoney(receive,total);
        return CommonResult.success(bean.transform());
    }

    /**
     * 获取最新收款信息
     * @return
     */
    @RequestMapping(value = "/home/getTopPayment")
    @ResponseBody
    public CommonList<Payment> getTopPayment(){
        EntityWrapper<Payment> entityWrapper = new EntityWrapper<>();
        entityWrapper.orderBy("crtime",false);
        entityWrapper.last("limit 5");
        List<Payment> list = paymentService.selectList(entityWrapper);
        return CommonList.success(list);
    }

    /**
     * 获取最新合同信息
     * @return
     */
    @RequestMapping(value = "/home/getTopContract")
    @ResponseBody
    public CommonList<Contract> getTopContract(){
        EntityWrapper<Contract> entityWrapper = new EntityWrapper<>();
        entityWrapper.orderBy("crtime",false);
        entityWrapper.last("limit 5");
        List<Contract> list = contractService.selectList(entityWrapper);
        return CommonList.success(list);
    }

    /**
     * 获取最新客户信息
     * @return
     */
    @RequestMapping(value = "/home/getTopCustomer")
    @ResponseBody
    public CommonList<Customer> getTopCustomer(){
        EntityWrapper<Customer> entityWrapper = new EntityWrapper<>();
        entityWrapper.orderBy("id",false);
        entityWrapper.last("limit 5");
        List<Customer> list = customerService.selectList(entityWrapper);
        return CommonList.success(list);
    }
}
